package onjava.advance.ch01.sec04;

import onjava.OSExecute;

import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Set;
import java.util.TreeSet;

enum Explore {HERE, THERE}

/**
 * values()方法的神秘之处
 * 1、所有枚举类型都继承自Enum
 * 2、Enum类中并没有values()方法
 * 3、枚举类型中的values()方法是由编译器添加的静态就去
 * 4、枚举类被限定为final，无法被继承
 *
 * @author yangdc
 * @date 9/19/25 23:46
 */
public class Reflection {
    public static Set<String> analyze(Class<?> enumClass) {
        System.out.println("______Analyzing " + enumClass + "______");
        System.out.println("Interfaces:");
        for (Type t : enumClass.getGenericInterfaces()) {
            System.out.println(t);
        }
        System.out.println("Base: " + enumClass.getSuperclass());
        System.out.println("Methods: ");
        Set<String> methods = new TreeSet<>();
        for (Method m : enumClass.getMethods()) {
            methods.add(m.getName());
        }
        System.out.println(methods);
        return methods;
    }

    public static void main(String[] args) {
        Set<String> exploreMethods = analyze(Explore.class);
        Set<String> enumMethods = analyze(Enum.class);
        System.out.println("Explore.containsAll(Enum)? " + exploreMethods.removeAll(enumMethods));
        System.out.println(exploreMethods);
        // 反编译enum，注意路径
        OSExecute.command("javap -cp target/classes onjava.advance.ch01.sec04.Explore");
    }
}
